package com.senter.demo.uhf;

import com.senter.demo.uhf.App.Protocol;
import com.senter.demo.uhf.modelD2.ConfigurationSettingsOfModelD2;
import com.senter.support.openapi.StUhf;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * 主界面
 * 
 * @author
 * @version 创建时间：2013-7-11 上午8:37:18
 */
public class Main2Activity extends Activity
{
	public static final String		Tag				= "Main2Activity";

	private ConfigurationSettingsOfModelD2	mConfigurationSettingsOfModelD2;
	
	private Protocol					mCurrentProtocolOfModelD2	= null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{

		mConfigurationSettingsOfModelD2 = App.getInstance().getConfigurationSettingsOfModelD2();

		super.onCreate(savedInstanceState);

		if (App.getRfid() == null)
		{
			Toast.makeText(this, R.string.MakeSurePDAexitRfid, Toast.LENGTH_LONG).show();
			finish();
		}
	}

	@Override
	protected void onResume()
	{
		onCreateInitViews();
		super.onResume();
	}

	private class Views
	{
	}

	private class ViewsAB extends Views
	{
		public ViewsAB() {
			setContentView(R.layout.activitymain);

			// GridLayout gl = (GridLayout) findViewById(R.id.idMain2Activity_glApps);

			LinearLayout ll = (LinearLayout) findViewById(R.id.idMain2Activity_glApps_inInventory);
			ll.setOnClickListener(new View.OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C0Inventory();
				}
			});

			ll = (LinearLayout) findViewById(R.id.idMain2Activity_glApps_inRead);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C1Read();
				}
			});

			ll = (LinearLayout) findViewById(R.id.idMain2Activity_glApps_inWrite);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C2Write();
				}
			});
			ll = (LinearLayout) findViewById(R.id.idMain2Activity_glApps_inErease);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C3Erase();
				}
			});
			ll = (LinearLayout) findViewById(R.id.idMain2Activity_glApps_inLock);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C4Lock();
				}
			});
			ll = (LinearLayout) findViewById(R.id.idMain2Activity_glApps_inUnlock);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C5UnLock();
				}
			});
			ll = (LinearLayout) findViewById(R.id.idMain2Activity_glApps_inKill);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C6Kill();
				}
			});
			ll = (LinearLayout) findViewById(R.id.idMain2Activity_glApps_inSettings);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					onSettings();
				}
			});
		}
	}

	private class ViewsC extends Views
	{
		public ViewsC() {
			setContentView(R.layout.activitymain);

			// GridLayout gl = (GridLayout) findViewById(R.id.idMain2Activity_glApps);

			LinearLayout ll = (LinearLayout) findViewById(R.id.idMain2Activity_glApps_inInventory);
			ll.setOnClickListener(new View.OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C0Inventory();
				}
			});

			ll = (LinearLayout) findViewById(R.id.idMain2Activity_glApps_inRead);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C1Read();
				}
			});

			ll = (LinearLayout) findViewById(R.id.idMain2Activity_glApps_inWrite);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C2Write();
				}
			});
			ll = (LinearLayout) findViewById(R.id.idMain2Activity_glApps_inErease);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C3Erase();
				}
			});
			ll.setVisibility(View.GONE);

			ll = (LinearLayout) findViewById(R.id.idMain2Activity_glApps_inLock);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C4Lock();
				}
			});
			ll = (LinearLayout) findViewById(R.id.idMain2Activity_glApps_inUnlock);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C5UnLock();
				}
			});
			ll = (LinearLayout) findViewById(R.id.idMain2Activity_glApps_inKill);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C6Kill();
				}
			});
			ll = (LinearLayout) findViewById(R.id.idMain2Activity_glApps_inSettings);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					onSettings();
				}
			});
		}
	}

	private class ViewsD2
	{

		public ViewsD2() {// idMain2ActivityD_glApps_6b_inInventory

			if ((mConfigurationSettingsOfModelD2.usingProtocol()==Protocol.I8k6c) && (mCurrentProtocolOfModelD2==Protocol.I8k6c))
			{
				return;
			} else if ((mConfigurationSettingsOfModelD2.usingProtocol()==Protocol.I8k6b) && (mCurrentProtocolOfModelD2==Protocol.I8k6b))
			{
				return;
			} else
			{
				// 初始化
			}

			setContentView(R.layout.activitymain_d);

			View tv6CTag, tv6BTag;
			View glApps6C, glApps6B;

			LinearLayout ll;

			tv6CTag = findViewById(R.id.idMain2ActivityD6C_tv6CTag);
			tv6BTag = findViewById(R.id.idMain2ActivityD6C_tv6BTag);
			glApps6C = findViewById(R.id.idMain2ActivityD6C_glApps);
			glApps6B = findViewById(R.id.idMain2ActivityD6B_glApps);

			if (mConfigurationSettingsOfModelD2.usingProtocol()==Protocol.I8k6c)
			{// 6C
				mCurrentProtocolOfModelD2 = Protocol.I8k6c;

				tv6BTag.setVisibility(View.GONE);
				glApps6B.setVisibility(View.GONE);

				tv6CTag.setVisibility(View.VISIBLE);
				glApps6C.setVisibility(View.VISIBLE);

				ll = (LinearLayout) findViewById(R.id.idMain2ActivityD_glApps_inInventory);
				ll.setOnClickListener(new View.OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						on6C0Inventory();
					}
				});

				ll = (LinearLayout) findViewById(R.id.idMain2ActivityD_glApps_inRead);
				ll.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						on6C1Read();
					}
				});

				ll = (LinearLayout) findViewById(R.id.idMain2ActivityD_glApps_inWrite);
				ll.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						on6C2Write();
					}
				});
				ll = (LinearLayout) findViewById(R.id.idMain2ActivityD_glApps_inErease);
				ll.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						on6C3Erase();
					}
				});
				ll = (LinearLayout) findViewById(R.id.idMain2ActivityD_glApps_inLock);
				ll.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						on6C4Lock();
					}
				});
				ll = (LinearLayout) findViewById(R.id.idMain2ActivityD_glApps_inKill);
				ll.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						on6C6Kill();
					}
				});

			} else if (mConfigurationSettingsOfModelD2.usingProtocol()==Protocol.I8k6b)
			{// 6B

				mCurrentProtocolOfModelD2 = Protocol.I8k6b;
				tv6BTag.setVisibility(View.VISIBLE);
				glApps6B.setVisibility(View.VISIBLE);

				tv6CTag.setVisibility(View.GONE);
				glApps6C.setVisibility(View.GONE);

				ll = (LinearLayout) findViewById(R.id.idMain2ActivityD6b_glApps_inInventory);
				ll.setOnClickListener(new View.OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						on6B0Inventory();
					}
				});

				ll = (LinearLayout) findViewById(R.id.idMain2ActivityD6b_glApps_inRead);
				ll.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						on6B1Read();
					}
				});

				ll = (LinearLayout) findViewById(R.id.idMain2ActivityD6b_glApps_inWrite);
				ll.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						on6B2Write();
					}
				});

				ll = (LinearLayout) findViewById(R.id.idMain2ActivityD6b_glApps_inLock);
				ll.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						on6B3Lock();
					}
				});
				ll = (LinearLayout) findViewById(R.id.idMain2ActivityD6b_glApps_inUnlock);
				ll.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						on6B4QuearyLock();
					}
				});
			} else
			{
				// 只有两种协议 不会到达此处
			}

			ll = (LinearLayout) findViewById(R.id.idMain2ActivityD_glApps_inSettings);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					onSettings();
				}
			});
		}
	}

	private class ViewsE
	{
		
		public ViewsE()
		{//idMain2ActivityD_glApps_6b_inInventory
			
			setContentView(R.layout.activitymain_e);
			
			LinearLayout ll = (LinearLayout) findViewById(R.id.idMain2ActivityE_glApps_inInventory);
			ll.setOnClickListener(new View.OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C0Inventory();
				}
			});

			ll = (LinearLayout) findViewById(R.id.idMain2ActivityE_glApps_inRead);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C1Read();
				}
			});

			ll = (LinearLayout) findViewById(R.id.idMain2ActivityE_glApps_inWrite);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C2Write();
				}
			});
			ll = (LinearLayout) findViewById(R.id.idMain2ActivityE_glApps_inErease);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C3Erase();
				}
			});
			ll = (LinearLayout) findViewById(R.id.idMain2ActivityE_glApps_inLock);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C4Lock();
				}
			});
			ll = (LinearLayout) findViewById(R.id.idMain2ActivityE_glApps_inKill);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					on6C6Kill();
				}
			});
			ll = (LinearLayout) findViewById(R.id.idMain2ActivityE_glApps_inSettings);
			ll.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					onSettings();
				}
			});
		}
	}
	
	private void onCreateInitViews()
	{
		if (App.getRfid()==null)
		{
			return;
		}
		
		switch (App.getRfid().getInterrogatorModel())
		{
			case InterrogatorModelA:
			case InterrogatorModelB:
			{
				new ViewsAB();
				break;
			}
			case InterrogatorModelC:
			{
				new ViewsC();
				break;
			}
			case InterrogatorModelD2:
			{
				new ViewsD2();
				break;
			}
			case InterrogatorModelE:
			{
				new ViewsE();
				break;
			}
			default:
				break;
		}
	}

	protected void on6C0Inventory()
	{
		switch (App.rfid().getInterrogatorModel())
		{
			case InterrogatorModelA:
				myStartActivity(com.senter.demo.uhf.modelA.Activity0Inventory.class);
				break;
			case InterrogatorModelB:
				myStartActivity(com.senter.demo.uhf.modelB.Activity0Inventory.class);
				break;
			case InterrogatorModelC:
				myStartActivity(com.senter.demo.uhf.modelC.Activity0Inventory.class);
				break;
			case InterrogatorModelD1:
			case InterrogatorModelD2:
				myStartActivity(com.senter.demo.uhf.modelD2.Activity0Inventory.class);
				break;
			case InterrogatorModelE:
				myStartActivity(com.senter.demo.uhf.modelE.Activity0Inventory.class);
				break;
			default:
				break;
		}
	}

	protected void on6C1Read()
	{
		switch (App.rfid().getInterrogatorModel())
		{
			case InterrogatorModelA:
				myStartActivity(com.senter.demo.uhf.modelA.Activity1Read.class);
				break;
			case InterrogatorModelB:
				myStartActivity(com.senter.demo.uhf.modelB.Activity1Read.class);
				break;
			case InterrogatorModelC:
				myStartActivity(com.senter.demo.uhf.modelC.Activity1Read.class);
				break;
			case InterrogatorModelD1:
			case InterrogatorModelD2:
				myStartActivity(com.senter.demo.uhf.modelD2.Activity1Read.class);
				break;
			case InterrogatorModelE:
				myStartActivity(com.senter.demo.uhf.modelE.Activity1Read.class);
				break;
			default:
				break;
		}
	}

	protected void on6C2Write()
	{
		switch (App.rfid().getInterrogatorModel())
		{
			case InterrogatorModelA:
				myStartActivity(com.senter.demo.uhf.modelA.Activity2Write.class);
				break;
			case InterrogatorModelB:
				myStartActivity(com.senter.demo.uhf.modelB.Activity2Write.class);
				break;
			case InterrogatorModelC:
				myStartActivity(com.senter.demo.uhf.modelC.Activity2Write.class);
				break;
			case InterrogatorModelD1:
			case InterrogatorModelD2:
				myStartActivity(com.senter.demo.uhf.modelD2.Activity2Write.class);
				break;
			case InterrogatorModelE:
				myStartActivity(com.senter.demo.uhf.modelE.Activity2Write.class);
				break;
			default:
				break;
		}
	}

	protected void on6C3Erase()
	{
		switch (App.rfid().getInterrogatorModel())
		{
			case InterrogatorModelA:
				myStartActivity(com.senter.demo.uhf.modelA.Activity3Erase.class);
				break;
			case InterrogatorModelB:
				myStartActivity(com.senter.demo.uhf.modelB.Activity3Erase.class);
				break;
			default:
				break;
		}
	}

	protected void on6C4Lock()
	{
		switch (App.rfid().getInterrogatorModel())
		{
			case InterrogatorModelA:
				myStartActivity(com.senter.demo.uhf.modelA.Activity4Lock.class);
				break;
			case InterrogatorModelB:
				myStartActivity(com.senter.demo.uhf.modelB.Activity4Lock.class);
				break;
			case InterrogatorModelC:
				myStartActivity(com.senter.demo.uhf.modelC.Activity4Lock.class);
				break;
			case InterrogatorModelD1:
			case InterrogatorModelD2:
				myStartActivity(com.senter.demo.uhf.modelD2.Activity3Lock.class);
				break;
			case InterrogatorModelE:
				myStartActivity(com.senter.demo.uhf.modelE.Activity3Lock.class);
				break;
			default:
				break;
		}
	}

	protected void on6C5UnLock()
	{
		switch (App.rfid().getInterrogatorModel())
		{
			case InterrogatorModelA:
				myStartActivity(com.senter.demo.uhf.modelA.Activity5Unlock.class);
				break;
			case InterrogatorModelB:
				myStartActivity(com.senter.demo.uhf.modelB.Activity5Unlock.class);
				break;
			case InterrogatorModelC:
				myStartActivity(com.senter.demo.uhf.modelC.Activity5Unlock.class);
				break;
			default:
				break;
		}
	}

	protected void on6C6Kill()
	{
		switch (App.rfid().getInterrogatorModel())
		{
			case InterrogatorModelA:
				myStartActivity(com.senter.demo.uhf.modelA.Activity6Kill.class);
				break;
			case InterrogatorModelB:
				myStartActivity(com.senter.demo.uhf.modelB.Activity6Kill.class);
				break;
			case InterrogatorModelC:
				myStartActivity(com.senter.demo.uhf.modelC.Activity6Kill.class);
				break;
			case InterrogatorModelD1:
			case InterrogatorModelD2:
				myStartActivity(com.senter.demo.uhf.modelD2.Activity4Kill.class);
				break;
			case InterrogatorModelE:
				myStartActivity(com.senter.demo.uhf.modelE.Activity4Kill.class);
				break;
			default:
				break;
		}
	}

	protected void onSettings()
	{
		switch (App.rfid().getInterrogatorModel())
		{
			case InterrogatorModelA:
				myStartActivity(com.senter.demo.uhf.modelA.Activity7Settings.class);
				break;
			case InterrogatorModelB:
				myStartActivity(com.senter.demo.uhf.modelB.Activity7Settings.class);
				break;
			case InterrogatorModelC:
				myStartActivity(com.senter.demo.uhf.modelC.Activity7Settings.class);
				break;
			case InterrogatorModelD1:
			case InterrogatorModelD2:
				myStartActivity(com.senter.demo.uhf.modelD2.Activity5Settings.class);
				break;
			case InterrogatorModelE:
				myStartActivity(com.senter.demo.uhf.modelE.Activity5Settings.class);
				break;
			default:
				break;
		}
	}

	protected void on6B0Inventory()
	{
		switch (App.rfid().getInterrogatorModel())
		{
			case InterrogatorModelD2:
				myStartActivity(com.senter.demo.uhf.modelD2.iso18k6b.Activity0Inventory.class);
				break;
			default:
				break;
		}
	}

	protected void on6B1Read()
	{

		switch (App.rfid().getInterrogatorModel())
		{
			case InterrogatorModelD2:
				myStartActivity(com.senter.demo.uhf.modelD2.iso18k6b.Activity1Read.class);
				break;
			default:
				break;
		}
	}

	protected void on6B2Write()
	{

		switch (App.rfid().getInterrogatorModel())
		{
			case InterrogatorModelD2:
				myStartActivity(com.senter.demo.uhf.modelD2.iso18k6b.Activity2Write.class);
				break;
			default:
				break;
		}
	}

	protected void on6B3Lock()
	{

		switch (App.rfid().getInterrogatorModel())
		{
			case InterrogatorModelD2:
				myStartActivity(com.senter.demo.uhf.modelD2.iso18k6b.Activity3Lock.class);
				break;
			default:
				break;
		}
	}

	protected void on6B4QuearyLock()
	{

		switch (App.rfid().getInterrogatorModel())
		{
			case InterrogatorModelD2:
				myStartActivity(com.senter.demo.uhf.modelD2.iso18k6b.Activity4QueryLock.class);
				break;
			default:
				break;
		}
	}

	private void myStartActivity(	Class<?> clazz)
	{
		startActivity(new Intent(this, clazz));
	}

	@Override
	public boolean onKeyDown(	int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			StUhf rfid = App.rfid();
			if (rfid != null)
			{
				rfid.uninit();
			}
			finish();

			android.os.Process.killProcess(android.os.Process.myPid());
		}
		return super.onKeyDown(keyCode, event);
	}
}
